package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.JxjlModel;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

/**
 * 检修记录详情页
 */
public class CheckRecordActivtity extends BaseActivity {

    private TitleView titleView;
    private Db db;

    private InfoTextView tv_check_time, tv_check_grade,
            tv_people, tv_check_descri, tv_instuctor, tv_note;
    private LinearLayout ll_jxjl;
    private static final String TAG = "CheckRecordActivtity";
    JxjlModel model;
    private int i = -1;
    private DownAttachmentDialogUtil dialogUtil;


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jxjlxq);
        initviews();
        db = Db.getInstance(this);
        model = (JxjlModel) getIntent().getSerializableExtra("jxjl");
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "检修记录");
        Log.e("1231", form_CODE);
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");


//        int i = model.jxsj.lastIndexOf(".");
//        if (-1 != i){
//            tv_check_time.setText(model.jxsj.substring(0,i));
//        }else{
        tv_check_time.setText(model.maintenance_time);
//        }
// 检修类型没有
// String name1 = db.getName("CODE_NAME","TB_SYS_CODE","CODE",model.jxlx);
//        tv_check_type.setText(name1);
        String name2 = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.level_id);
        tv_check_grade.setText(name2);

        String name = db.getName("PERS_NAME", "TB_SYS_PERSON", "GUID", model.supervisor_id);
        tv_people.setText(name);
        tv_check_descri.setText(model.maintenance_describe);
        tv_note.setText(model.note);
        Log.e("tv_attachment", model.attachment);
        String name3 = db.getName("WJMC", "TB_FILE_LB", "GUID", model.attachment);
        if (!StringUtils.isNullOrEmpty(name3)) {
            tv_instuctor.setText(name3);
            i = 1;
            Log.e("tv_attachment", name3);
        } else {
            String name5 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.attachment);
            Log.e("tv_attachment", name5);
            tv_instuctor.setText(name5);
            i = 2;
        }

        //作业指导书 检修资料 附件路径不全

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                ll_jxjl.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);
                //设置value的值
                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);
//                String zdyzdValue = db.getZdyzdValue(model1.guid, model.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
//-------------------------------------------------------------------------------------------------------


        fillFakeData();
    }

    private void initviews() {
        titleView = (TitleView) this.findViewById(R.id.titleView);
        tv_check_time = (InfoTextView) findViewById(R.id.tv_check_time);
        tv_check_grade = (InfoTextView) findViewById(R.id.tv_check_grade);
        tv_people = (InfoTextView) findViewById(R.id.tv_people);
        tv_check_descri = (InfoTextView) findViewById(R.id.tv_check_descri);
        tv_instuctor = (InfoTextView) findViewById(R.id.tv_instuctor);
        tv_note = (InfoTextView) findViewById(R.id.tv_note);
        ll_jxjl = (LinearLayout) findViewById(R.id.ll_jxjl);

        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });
    }

    /**
     * 填充假数据
     */
    private void fillFakeData() {

        tv_instuctor.setOnClickListener(new View.OnClickListener(
        ) {
            public void onClick(View view) {
                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.attachment);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.attachment);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_instuctor.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    final String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(CheckRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(CheckRecordActivtity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(CheckRecordActivtity.this, guid, model.attachment, fjlx).execute();
                                                dialogUtil.dismiss();

                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();

                    } else {
                        IntentUtils.JumpToIntent(CheckRecordActivtity.this, fjlx, filePath);

                    }



                }


            }
        });
    }

//        tv_check_info.setOnClickListener(new View.OnClickListener(
//        ){
//            public void onClick(View view) {
//                Log.i("点击测试", "收到点击事件");
//                String filePath = AppConfig.ATTACHMENT_PATh+tv_check_info.getText();
//                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_check_info.getText());
//                Log.e("LLL", String.valueOf(nullOrEmpty));
//                if(nullOrEmpty)
//                    return;
//                File file = new File(filePath);
//                if(!file.exists()){
//                    Toast.makeText(CheckRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
//                }
//                else{
//                    Intent intent = MyIntent.getPdfFileIntent(filePath);
//                    startActivity(intent);
//                }
//            }
//        });
}
